You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adds support for folding and unfolding inactive preprocessor regions.
Adds the C/C++: Fold All Inactive Regions and C/C++: Unfold All Inactive Regions commands for the active C, C++, or CUDA file.
Adds the opt-in C_Cpp.autoFoldInactiveRegions setting to fold inactive regions after IntelliSense finishes processing a newly opened editor.
Keeps folding independent of inactive-region colorization, allowing regions to be folded when C_Cpp.dimInactiveRegions is disabled.
Leaves existing behavior unchanged by default.
Implementation
Inactive ranges may arrive across multiple IntelliSense result notifications. This change accumulates them per document and waits for a complete IntelliSense pass before folding. This prevents an incomplete result from selecting a nested folding range instead of the surrounding inactive preprocessor branch.
Manual requests made before IntelliSense results are ready remain pending until a complete result arrives. Cached ranges are invalidated when a document changes, opens, or closes. Automatic folding occurs only once per editor instance, including across language-client recovery. Cached inactive-region decorations are refreshed immediately when their appearance settings change, and folding state is routed to the URI-owning client in multi-root workspaces.
Testing
Added unit coverage for accumulating complete inactive-region results, replacing and deleting cached results, and producing sorted unique folding lines.
Added SingleRootProject scenario coverage for manual fold/unfold, disabled dimming, automatic folding, disabled code folding and enable-later retry, pending requests before IntelliSense is ready, language-client recovery, and empty initial inactive-region results.
Added multi-root scenario coverage for URI-based fold/unfold command routing.
Manual folds incorrectly suppress later automatic folding
Extension/src/LanguageServer/client.ts:3018
A manual fold also marks the editor as already auto-folded. If the user manually folds, unfolds, and then enables autoFoldInactiveRegions, the settings-change path will refuse to auto-fold this editor even though no automatic fold occurred. Only record the editor when autoFold is actually true.
Route document command using the current editor URI
Extension/src/LanguageServer/extension.ts:931
Route this document-scoped command from the current editor URI. ClientCollection.didChangeActiveEditor updates ActiveClient only after awaiting the new client's editor-change notification, so invoking this command during a multi-root editor switch can add the pending fold to the previous client; the actual owner receives the ranges and never fulfills that request.
The reason will be displayed to describe this comment to others. Learn more.
Is C_Cpp.FoldInactiveRegion needed given that VS Code itself appears to have a whole set of fold/unfold command features.
UPDATE: Oh, well the VS Code commands apply to non-inactive regions too. So, should there be a C_Cpp.UnfoldInactiveRegion command to match what VS Code supports and to undo the Fold command?
Track pending folding operations per editor, not document URI
Extension/src/LanguageServer/client.ts:3005
Pending manual operations are keyed only by document URI, although folding state is per TextEditor. If the same document is open in two editor groups and the user requests a fold before results are complete, switching to the other view causes tryApplyInactiveRegionFolding to consume the request there; the original view remains unfolded (and a request from the second view can overwrite the first). Track pending operations by editor instance (for example, a WeakMap<vscode.TextEditor, ...>) and consume only the operation belonging to the active editor.
Mark the editor as auto-folded before returning for an empty complete result. Otherwise, a file opened with no inactive branches remains absent from autoFoldedEditors, so a later edit that introduces an inactive branch triggers automatic folding even though automatic folding is documented to happen on open and only once per editor instance.
(copilot)
Addressed the latest Copilot review in cd241ac. A completed initial pass now marks the editor as auto-folded even when it contains no inactive regions, so a later edit cannot trigger unexpected on-open folding. Added a scenario that opens main.cpp with an empty result, introduces an inactive block, waits for IntelliSense, and verifies it remains unfolded.
The reason will be displayed to describe this comment to others. Learn more.
Copilot review overview
🔵 Needs a closer look
Enabling code folding does not retry automatic folding for an already-open eligible editor.
Review effort: Balanced Findings: None
Previously missed (1)
In code that hasn't changed since last review
Retry automatic folding when code folding is enabled later
Extension/src/LanguageServer/client.ts:1911
Re-evaluate automatic folding when codeFolding changes too. If a file is opened while autoFoldInactiveRegions is true but code folding is disabled, enabling code folding later leaves the active editor unfolded because the cached complete ranges are never retried (and the outgoing inactive-range flag was already enabled by autoFoldInactiveRegions, so this setting change need not produce another IntelliSense result).
(copilot)
Addressed the latest Copilot review in 8c864c4. Changes to C_Cpp.codeFolding now fire the language client folding provider change event before reevaluating automatic folding, so VS Code rebuilds its folding model before editor.fold runs. The regression scenario opens with auto-fold enabled and code folding disabled, verifies the region remains unfolded, enables code folding, and verifies it then folds.
The reason will be displayed to describe this comment to others. Learn more.
Copilot review overview
🔵 Needs a closer look
Toggling inactive-region dimming can leave stale decorations in already-open editors.
Review effort: Balanced Findings: None
Previously missed (1)
In code that hasn't changed since last review
Refresh inactive-region decorations when dimInactiveRegions changes
Extension/src/LanguageServer/client.ts:1914
With codeFolding enabled (the default), the native dimInactiveRegions flag now remains true even when the user toggles dimming. This settings-change path does not update inactive-region decorations for dimInactiveRegions, so an already-open editor can retain stale dimming (or fail to gain it) until another IntelliSense result happens to arrive. Handle dimInactiveRegions changes here by immediately disposing or rebuilding decorations from the cached ranges while keeping native range reporting enabled independently.
Addressed the latest Copilot review in c5fd362. Inactive-region decorations are now stored on the URI-owning workspace client and immediately disposed or rebuilt from cached ranges when dimming, opacity, foreground, or background settings change. Decoration types are also disposed when documents close or clients are replaced.
The reason will be displayed to describe this comment to others. Learn more.
✨Copilot (agent46): [Minor] The earlier Copilot review body raised this, but the current head still queues a manual fold/unfold by document URI rather than by the TextEditor that requested it. If the same file is open in two editor groups, a command queued in view A before inactive ranges are complete can be consumed after focus moves to view B; the editor-scoped editor.fold/editor.unfold command then targets B instead. Could you keep pending requests associated with their originating editor (or cancel on a view change) and cover the split-view case?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds support for folding and unfolding inactive preprocessor regions.
C/C++: Fold All Inactive RegionsandC/C++: Unfold All Inactive Regionscommands for the active C, C++, or CUDA file.C_Cpp.autoFoldInactiveRegionssetting to fold inactive regions after IntelliSense finishes processing a newly opened editor.C_Cpp.dimInactiveRegionsis disabled.Implementation
Inactive ranges may arrive across multiple IntelliSense result notifications. This change accumulates them per document and waits for a complete IntelliSense pass before folding. This prevents an incomplete result from selecting a nested folding range instead of the surrounding inactive preprocessor branch.
Manual requests made before IntelliSense results are ready remain pending until a complete result arrives. Cached ranges are invalidated when a document changes, opens, or closes. Automatic folding occurs only once per editor instance, including across language-client recovery. Cached inactive-region decorations are refreshed immediately when their appearance settings change, and folding state is routed to the URI-owning client in multi-root workspaces.
Testing
Fixes #8992